package de.hpi.generator.config;

import java.util.ArrayList;

public class DependencyModels extends ArrayList<DependencyModel> {

    public DependencyModels getNormalizedInclusionDependencyModels() {
        DependencyModels result = new DependencyModels();

        for (DependencyModel aInclusionDependencyModel : this) {
            if (isCompositeInclusionDependency(aInclusionDependencyModel)) {
                DependencyModel aCompositeDependency = createNormalizedCompositeInclusionDependency(aInclusionDependencyModel);
                result.addUniqueItem(aCompositeDependency);
            } else {
                DependencyModels aSingleDepencySet = generateSingleDepencySet(aInclusionDependencyModel);
                result.addUniqueItems(aSingleDepencySet);
            }
        }
        return result;
    }

    protected boolean isCompositeInclusionDependency(DependencyModel aInclusionDependencyModel) {
        return aInclusionDependencyModel.dependent.contains("(") || aInclusionDependencyModel.referenced.contains("(");
    }

    protected DependencyModel createNormalizedCompositeInclusionDependency(DependencyModel aInclusionDependencyModel) {
        String aDependent = aInclusionDependencyModel.dependent.replace("(", "").replace(")", "");
        String aReferenced = aInclusionDependencyModel.referenced.replace("(", "").replace(")", "");
        DistributionModel aDistribution = aInclusionDependencyModel.distribution;

        DependencyModel result = new DependencyModel(aDependent, aReferenced, aDistribution);
        result.setComposite(true);
        return result;
    }

    protected void addUniqueItems(DependencyModels aDepencySet) {
        for (DependencyModel aDependency : aDepencySet) {
            addUniqueItem(aDependency);
        }
    }

    protected void addUniqueItem(DependencyModel aDependency) {
        if (!isExists(aDependency)) {
            this.add(aDependency);
        }
    }

    protected boolean isExists(DependencyModel aInclusionDependencyModel) {
        for (DependencyModel aTestObject : this) {
            if (aTestObject.dependent.compareTo(aInclusionDependencyModel.dependent) == 0
                    && aTestObject.referenced.compareTo(aInclusionDependencyModel.referenced) == 0) {
                return true;
            }
        }
        return false;
    }

    protected DependencyModels generateSingleDepencySet(DependencyModel aInclusionDependencyModel) {
        DependencyModels result = new DependencyModels();

        String[] aLHSPartItems = aInclusionDependencyModel.dependent.replace(" ", "").split(",");
        String[] aRHSPartItems = aInclusionDependencyModel.referenced.replace(" ", "").split(",");
        for (String aLHSPartItem : aLHSPartItems) {
            for (String aRHSPartItem : aRHSPartItems) {
                if (aLHSPartItem.compareTo(aRHSPartItem) != 0) {
                    result.add(new DependencyModel(aLHSPartItem, aRHSPartItem));
                }
            }
        }
        return result;
    }
}
